﻿using System;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using RStein.Posterous.API;
using RStein.PosterousReader.WP.HostServices;
using RStein.PosterousReader.WP.Extensions;
using RStein.PosterousReader.WP.ViewModels.ViewObjects;
using System.Threading;

namespace RStein.PosterousReader.WP.ViewModels
{
  public class PostEditContainerViewModel : PostAddContainerViewModel
  {
    
    private bool m_isPostLoaded;
    
    public PostEditContainerViewModel(IPosterousApplication posterousApplication, INavigationService navigation)
      : base(posterousApplication, navigation, GlobalConstants.PAGE_EDIT_POST_TITLE)
    {

    }

    public bool IsPostLoadingInProgress
    {
      get
      {
        return m_isPostLoaded;
      }
      set
      {
        m_isPostLoaded = value;
        RaisePropertyChangedEvent(() => IsPostLoadingInProgress);
        RaisePropertyChangedEvent(() => SaveCommand);
      }

    }
    protected override void DoInternalInit()
    {
      base.DoInternalInit();
      IsPostLoadingInProgress = true;      
      CurrentPost = new PostVO
      {
        SynchContext = SynchronizationContext.Current,        
        Body = GlobalConstants.DEFAULT_LOADING_MESSAGE,
        Title = GlobalConstants.DEFAULT_LOADING_MESSAGE
      };
                                
      
      
    }
    
    protected override void  DoInternalAsyncInit()
    {
      try
      {
        base.DoInternalAsyncInit();
        IPosterousPost origPost = getPosterousPost();

        CurrentPost = new PostVO
                      {
                        SynchContext = SynchronizationContext.Current,
                        Id = origPost.Id,
                        Body = origPost.Body,
                        Title = origPost.Title
                      };
                                
      }
      finally
      {
        IsPostLoadingInProgress = false;
      }
      
    }

    protected override IPosterousPost GetTargetPost(IPosterousSite targetSite)
    {
      return getPosterousPost();
    }       
    
    private IPosterousPost getPosterousPost()
    {

      string idString = NavigationService.GetQueryParameter(GlobalConstants.OBJECT_ID_KEY);

      int id = int.Parse(idString.Trim());
      
      IPosterousAccount account = PosterousApplicatonService.GetOrAddCachedPosterousAccount(LastUsedUserName, LastUsedPassword);

      IPosterousPost currentPost = (from post in account.Sites.Where(site => site.Id == TargetSiteId).First().Posts
                                    where post.Id == id
                                    select post).First();
      return currentPost;

    }

    protected override bool IsAllInitDataLoaded()
    {
      return !IsPostLoadingInProgress;
    }

  }
}
